﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml.Serialization;
using System.IO;

namespace Chapter05_Lab02
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length > 0)
                Serialize(new Person(Convert.ToInt32(args[0]), args[1], args[2], Convert.ToBoolean(args[3])));
            else
                Console.WriteLine(Deserialize().ToString());
        }

        public static void Serialize(Person p)
        {
            FileStream fs = new FileStream("teste.txt", FileMode.Create);

            XmlSerializer xs = new XmlSerializer(typeof(Person));

            xs.Serialize(fs, p);

            fs.Close();
        }

        public static Person Deserialize()
        {
            FileStream fs = new FileStream("teste.txt", FileMode.Open);

            XmlSerializer xs = new XmlSerializer(typeof(Person));

            Person t = (Person)xs.Deserialize(fs);

            fs.Close();

            return t;
        }

    }

    public class Person
    {
        public int age;
        public string name;
        public string country;

        [XmlAttribute]
        public bool married;

        /// <summary>
        /// A parameter-less constructor is required to serialize utilizing the XML method.
        /// </summary>
        public Person()
        {
        }

        public Person(int age, string name, string country, bool married)
        {
            this.age = age;
            this.name = name;
            this.country = country;
            this.married = married;
        }

        public override string ToString()
        {
            return String.Format("Age = {0}, Name = {1}, Country = {2}, Married = {3}", this.age, this.name, this.country, this.married);
        }
    }
}
